package com.jin.spirng_boot_learn.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * selector 是管理渠道channel
 * selector 管理 channel的状态 ,所以将channel的状态机注册到selector中
 */
public class LearnSelector {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
        socketChannel.finishConnect();
        send(socketChannel, "777777");
    }

    private static void send(SocketChannel sc, String data) throws IOException {
        byte[] req = data.getBytes();
        ByteBuffer byteBuffer = ByteBuffer.allocate(req.length);
        byteBuffer.put(req);
        byteBuffer.flip();
        sc.write(byteBuffer);
        System.out.println("Send 123 client successed");
    }

}